﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
//using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Diagnostics;

namespace Athena.Tools
{
    public class CommandTextBox : RichTextBox
    {
        protected List<string> _CommandHistory = new List<string>();

        public CommandTextBox()
            : base()
        {
            Text = ">> ";
        }

        protected override void OnKeyUp(KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Up || e.KeyCode == Keys.Down)
                SelectionStart = TextLength + 1;
        }

        protected override void OnKeyDown(KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Down)
                SelectionStart = TextLength - Lines[Lines.Length - 1].Length - 1;
        }

        public List<string> CommandHistory
        {
            get { return _CommandHistory; }
            set { _CommandHistory = value; }
        }
        
        int index = 0;
        
        protected override bool IsInputKey(Keys keyData)
        {
            try
            {
                if (keyData == Keys.Enter || keyData == Keys.Return)
                {
                    SelectionStart = TextLength;
                    return true;
                }
                if (_CommandHistory.Count > 0 && (keyData == Keys.Up || keyData == Keys.Down))
                {
                    //SetCursorOnLastChar();

                    if (keyData == Keys.Up)
                        ++index;
                    else
                        --index;

                    if (index > _CommandHistory.Count) index = _CommandHistory.Count;
                    if (index < 0) index = 0;

                    int numLines = Lines.Length;
                    if (TextLength - Lines[numLines - 1].Length + 3 < TextLength)
                        Text = Text.Remove(TextLength - Lines[numLines - 1].Length + 3);

                    if (index > 0)
                        Text += _CommandHistory[_CommandHistory.Count - index];

                    //SelectionStart = TextLength;
                    //ScrollToCaret();

                    //SetCursorOnLastChar();

                    //Text += Environment.NewLine + Environment.NewLine;

                    return true;
                }
            }
            catch (Exception)
            {
                Utility.SetMessage("Error running command window");
            }
            return base.IsInputKey(keyData);
        }
        /*
        private void SetCursorOnLastChar()
        {
            int i = TextLength - 1;
            while (i >= 0 && char.IsWhiteSpace(Text[i]))
                --i;
            SelectionStart = i;
            if (i < TextLength)
                Text.Remove(i);
        }*/

        public delegate void OnCommandExecutionDelegate(string text, EventArgs e);

        public event OnCommandExecutionDelegate OnCommandExecution;

        protected override void OnTextChanged(EventArgs e)
        {
            if (TextLength < 3) Text = ">> ";
            int i = Lines.Length - 1;

            try
            {
                if (Lines[i].Length == 0 && i > 0)
                {
                    string command = Lines[i - 1];
                    command = command.Substring(3);  //remove >>
                    OnCommandExecution(command, EventArgs.Empty);
                    Text += ">> ";
                    SelectionStart = TextLength;
                    ScrollToCaret();
                    Refresh();
                    _CommandHistory.Add(command);
                    index = 0;
                }
                else
                {
                    int len = Text.Length - Lines[i].Length + 2;
                    if (SelectionStart <= len)
                    {
                        Undo();
                        SelectionStart = TextLength;
                    }
                }
                //base.OnTextChanged(e);
            }
            catch (Exception)
            {
                //
            }
        }
    }
}
